package com.ccjq.shosestore.web.controller.shoppingcar;

import com.ccjq.shosestore.product.po.HomePo;
import com.ccjq.shosestore.product.service.HomeService;
import com.ccjq.shosestore.product.service.impl.HomeServiceImpl;
import com.ccjq.shosestore.product.t_productPo.productPo;
import com.ccjq.shosestore.shoppingcart.po.CarPo;
import com.ccjq.shosestore.shoppingcart.po.ProductCarPo;
import com.ccjq.shosestore.shoppingcart.service.impl.CarServiceImpl;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarServlet extends HttpServlet {
    //List<ProductCarPo> CarListSale = new ArrayList<ProductCarPo>();
    private static final String FALG_INSERT ="insertcar";//添加购物车
    private static final String FALG_SELECT = "selectcar";//查询购物车
    private static final String FALG_DELETE = "deletecar";//删除商品
    private static final String FALG_GOHOME = "gohome";//继续购物
    private static final String FALG_CHECK  = "checkcar";//检查商品数量
    private static final String FALG_CLOSE  = "closecar";//结算
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");//用户的登录信息（名字）
        int maxquantity = 0;
        String falg = request.getParameter("falg");
        HomeServiceImpl hsi = new HomeServiceImpl();
        CarServiceImpl carService = new CarServiceImpl();
        List<HomePo> homeList = hsi.selectAll();
        request.setAttribute("homeList",homeList);
        if(FALG_INSERT.equals(falg)){

            if(StringUtils.isNotBlank(userId)){
                String product_id = request.getParameter("product_id");
                int result = carService.selectStock(product_id);
                if(result>0){
                    CarPo carPo = new CarPo();
                    carPo.setProduct_id(product_id);
                    carPo.setQuantity(1);
                    carPo.setUser_id(userId);
                    int insert = carService.insertAll(carPo);
                    if(insert>0){
                        double total=0.0;
                        List<ProductCarPo> CarList = carService.selectCar(userId);
                        for(ProductCarPo pp:CarList){
                            total+=pp.getSale();//合计计算
                        }
                        HomeService homeService = new HomeServiceImpl();
                        List<productPo> homeListReMai = homeService.selectReMai();
                        session.setAttribute("homeListReMai",homeListReMai);
                        request.setAttribute("CarList",CarList);
                        request.setAttribute("total",total);
                       request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
                    }

                }else {
                    request.setAttribute("result",result);
                    request.getRequestDispatcher("/WEB-INF/jsp/home/homepageadmin.jsp").forward(request,response);
                    return;
                }
            }else {
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            }

        }else if(FALG_SELECT.equals(falg)){
            if(StringUtils.isNotBlank(userId)){
                double total=0.0;
                List<ProductCarPo> CarList = carService.selectCar(userId);//返回购物车集合
                 for(ProductCarPo pp:CarList){
                     total+=pp.getSale();//合计计算
                 }
                request.setAttribute("CarList",CarList);
                request.setAttribute("total",total);
                request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            }

        }else if(FALG_DELETE.equals(falg)){
            String shopping_cart_id = request.getParameter("shopping_cart_id");
            int result = carService.deleteCar(shopping_cart_id);
            if(result==1){
                double total=0.0;
                List<ProductCarPo> CarList = carService.selectCar(userId);
                for(ProductCarPo pp:CarList){
                    total+=pp.getSale();//合计计算
                }
                request.setAttribute("CarList",CarList);
                request.setAttribute("total",total);
                request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
            }
        }else if(FALG_GOHOME.equals(falg)){
            request.getRequestDispatcher("/WEB-INF/jsp/home/homepageadmin.jsp").forward(request,response);
        }else if(FALG_CHECK.equals(falg)){
            int quantity = Integer.parseInt(request.getParameter("quantity"));
            String product_id = request.getParameter("productId");
            String shopping_cart_id = request.getParameter("shopping_cart_id");
            maxquantity = carService.selectStock(product_id);//最大库存量
            if(quantity<=maxquantity && quantity>0){
                Map<String, Object> resultMap = new HashMap<String,Object>();
                int resuleUpdate = carService.updateCar(quantity,shopping_cart_id);
                if(resuleUpdate==1){
                    double total=0.0;
                    List<ProductCarPo> CarList = carService.selectCar(userId);
                    for(ProductCarPo pp:CarList){
                        total+=pp.getSale();//合计计算
                        if(pp.getShopping_cart_id().equals(shopping_cart_id)){
                            System.out.println("小计为：" + pp.getSale());
                            resultMap.put("obj",pp);
                        }
                    }
                    PrintWriter out = response.getWriter();
                    resultMap.put("code",1);
                    resultMap.put("total", total);
                    Gson gson = new Gson();
                    out.print(gson.toJson(resultMap));
                    out.flush();
                }
            }else{
                int i = 0;//状态码0-失败
                PrintWriter out = response.getWriter();
                out.write("{\"code\":"+i+",\"stock\":"+maxquantity+"}");
            }

        }else if(FALG_CLOSE.equals(falg)){
            double total=0.0;
            List<ProductCarPo> CarList = carService.selectCar(userId);
            for(ProductCarPo pp:CarList){
                total+=pp.getSale();//合计计算
            }
            request.setAttribute("total",total);
            request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/settleaccounts.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

}
